package myclient;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.StringTokenizer;

import javax.swing.*;

/**
 * @author JIAO
 */
public class TalkFrame{
	HashMap<String,File> fileMap = new HashMap<String, File>();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HashMap<String,TalkFrame> talkMap;
	public JTextArea area1;
	private JTextArea area2;
	private JButton btClose;
	private JButton btSend;
	@SuppressWarnings("unused")
	private String myID;
	private String myName;
	private String talkToID;
	@SuppressWarnings("unused")
	private String talkToName;
	PrintWriter writer;
	SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
	public JPanel pnRight;
	public JFrame fm;
	HashMap<String,JPanel> pnMap = new HashMap<String,JPanel>();
	TalkFrame(final String talkToID,final MainFrame mainFm){
		fm = new JFrame();
		fm.setResizable(false);
		this.myID = mainFm.myID;
		this.myName = mainFm.conn.name;
		this.talkToID = talkToID;
		System.out.println(talkToID);
		this.talkToName = mainFm.conn.list.get(talkToID).getName();
		this.talkMap = mainFm.talkMap;
		this.writer = mainFm.conn.writer;
		
		fm.getContentPane().setLayout(null);
		fm.getContentPane().setBackground(new Color(0,200,255));
		fm.setBounds(200,200,500,490);
		fm.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				talkMap.remove(talkToID);
				e.getWindow().dispose();
			}
		});
		fm.setFocusable(false);
		fm.setTitle("与："+mainFm.conn.list.get(talkToID).getName()+"聊天中");
		
		area1 = new JTextArea();
		area1.setLineWrap(true);
		JScrollPane scrollPane1 = new JScrollPane(area1);
		fm.getContentPane().add(scrollPane1);
		scrollPane1.setBounds(5, 80, 370, 220);
		
		
		area2 = new JTextArea();
		area2.setLineWrap(true);
		JScrollPane scrollPane2 = new JScrollPane(area2);
		fm.getContentPane().add(scrollPane2);
		scrollPane2.setBounds(5, 330, 370, 90);
		
		btClose = new JButton("关闭");
		fm.getContentPane().add(btClose);
		btClose.setBounds(240, 425, 60, 20);
		
		btSend = new JButton("发送");
		fm.getContentPane().add(btSend);
		btSend.setBounds(315, 425, 60, 20);
		
		btSend.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				sendMessage();
			}
			
		});
		btClose.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				talkMap.remove(talkToID);
				fm.dispose();
			}});
		area2.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==10&&e.isControlDown()){//用户按了control和enter键，发送信息
					sendMessage();
				}
			}
		});
		
		fm.setVisible(true);
		System.out.println(fm.getAlignmentX()+" "+fm.getAlignmentY());

		final JButton btSendFile = new JButton();
		btSendFile.setText("发送文件");
		btSendFile.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(mainFm.conn.list.get(talkToID).getStatus().equals("0")){
					JOptionPane.showMessageDialog(null, "此好友不在线，不能发送文件");
					return;
				}
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				if(JFileChooser.CANCEL_OPTION==chooser.showDialog(null, "选择"))
					return;
				File f = chooser.getSelectedFile();
				if(!f.canRead()){
					JOptionPane.showMessageDialog(null, "文件不可读");
					return;
				}
				PrintWriter writer = mainFm.conn.writer; 
				writer.println("server");
				writer.println("sendFile");
				writer.println(talkToID);
				writer.println(f.getName());
				System.out.println("File name-->"+f.getName());
				fileMap.put(f.getName(), f);
				
				
			}});
		btSendFile.setBounds(205, 306, 95, 20);
		fm.getContentPane().add(btSendFile);
		
		
		pnRight = new JPanel();
		pnRight.setBounds(392, 80, 79, 340);
		fm.getContentPane().add(pnRight);
	}
	
	/**用来向服务器发送信息
	 * @author jiao
	 * @return void
	 * */
	private void sendMessage(){
		if(area2.getText().length()==0)
			return;
		area1.append(myName+"  "+format.format(new java.util.Date())+":\n");
		area1.append(area2.getText()+"\n");
		writer.println(talkToID);
		StringTokenizer tokens = new StringTokenizer(area2.getText(),"\n",true);
		while(tokens.hasMoreTokens()){
			String str =tokens.nextToken();
			writer.print(str);
			System.out.print(str);
		}
		writer.println();
		writer.println("end");
		area2.setText("");
	}
	
	public void addPn(final String fileName){
		JLabel lbFile = new JLabel(fileName);
		JLabel lbGet = new JLabel("接收");
		lbGet.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				System.out.println("用户已经确认接收文件了");
				writer.println("server");
				writer.println("receiveFile");
				writer.println(talkToID);
				writer.println(fileName);
				writer.println(1);
			}
			
		});
		JLabel lbRefuse = new JLabel("拒绝");
		final JPanel pn = new JPanel();
		lbRefuse.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				System.out.println("用户已经拒绝接收文件了");
				writer.println("server");
				writer.println("receiveFile");
				writer.println(talkToID);
				writer.println(fileName);
				writer.println(0);
				pnMap.remove(pn);
				pnRight.remove(pn);
				fm.repaint();
				fm.setVisible(true);
			}
			
		});
		pn.setName(fileName);
		pn.setLayout(new GridLayout(3,1));
		pn.add(lbFile);
		pn.add(lbGet);
		pn.add(lbRefuse);
		pn.setSize(new Dimension(60,60));
		pnRight.add(pn);
		pnMap.put(fileName, pn);
		fm.repaint();
		fm.setVisible(true);
		
	}
	public JPanel makePn(final String fileName){
		JLabel lbFile = new JLabel(fileName);
		JLabel lbGet = new JLabel("接收");
		lbGet.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				System.out.println("用户已经确认接收文件了");
				writer.println("server");
				writer.println("receiveFile");
				writer.println(talkToID);
				writer.println(fileName);
				writer.println(1);
			}
			
		});
		JLabel lbRefuse = new JLabel("拒绝");
		lbRefuse.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				System.out.println("用户已经拒绝接收文件了");
				writer.println("server");
				writer.println("receiveFile");
				writer.println(talkToID);
				writer.println(fileName);
				writer.println(0);
			}
			
		});
		JPanel pn = new JPanel();
		pn.setLayout(new GridLayout(3,1));
		pn.add(lbFile);
		pn.add(lbGet);
		pn.add(lbRefuse);
		pn.setPreferredSize(new Dimension(70,60));
		return pn;
		
	}
}
